package com.lebang.medicalInquiry.controller;


import com.alibaba.fastjson.JSONObject;
import com.lebang.medicalInquiry.component.RedisComponent;
import com.lebang.medicalInquiry.constant.RedisKeyEnum;
import com.lebang.medicalInquiry.constant.ResponseEnum;
import com.lebang.medicalInquiry.response.Callback;
import com.lebang.medicalInquiry.response.JsonData;
import com.lebang.medicalInquiry.utils.SendSmsUtil;
import com.lebang.medicalInquiry.utils.TextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.Map;

/**
 * @version :1.0
 * @date ：Created in 2022/3/10 11:32
 */
@Api(tags = "验证码相关")
@RequestMapping("/api/code")
@RestController
@Validated
public class CodeApi {

    @Autowired
    private RedisComponent redisComponent;

    @Value("${aly.access_key_id}")
    private String accessKeyId;

    @Value("${aly.access_key_secret}")
    private String accessKeySecret;

    @Value("${aly.template_code}")
    private String templateCode;

    @Value("${aly.sign_name}")
    private String signName;

    @ApiOperation("通过手机号生成登录验证码")
    @GetMapping("/{type}")
    public JsonData createLoginCode(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(name = "type", value = "register,login", required = true) @NotBlank(message = "type不能为空") @PathVariable(value = "type") String type
            , @ApiParam(name = "phone", value = "手机号", required = true) @NotBlank(message = "手机号不能为空") @RequestParam(value = "phone") String phone) {
        try {

            int liveTime = -2;
            RedisKeyEnum lock;
            RedisKeyEnum code;
            switch (type) {
                case "login":
                    lock = RedisKeyEnum.login_lock;
                    code = RedisKeyEnum.login_code;
                    break;
                default:
                    return Callback.fail(ResponseEnum.FAIL.getCode(), "请求类型不允许");
            }
            //校验用户是否可以发送验证码
            if (redisComponent.getExTimeByKey(lock.getName() + phone) == liveTime) {
                // 设置存活时间300s , 一旦过期才可以继续生成并发送验证码
                // 生成6位数字的验证码

                String checkCode = String.valueOf((int) (Math.random() * 900000 + 100000));
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("code", checkCode);
                boolean b = SendSmsUtil.alySms(phone, signName, templateCode, jsonObject.toJSONString(), accessKeyId, accessKeySecret);
                if (!b) {
                    return Callback.fail(ResponseEnum.FAIL.getCode(), ResponseEnum.FAIL.getName());
                }

                redisComponent.setExTimeValue(code.getName() + phone, checkCode, 60);
                redisComponent.setExTimeValue(lock.getName() + phone, 1, 60);
                return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
            }
            return Callback.fail(ResponseEnum.FAIL.getCode(), "一分钟内不能重复发送验证码");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Callback.fail(ResponseEnum.UNEXCEPTION.getCode(), ResponseEnum.UNEXCEPTION.getMemo());
    }

}
